package com.david.func_test.widget

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetBehavior
import kotlin.math.abs

class DiscoverRecyclerView(context: Context, attrs: AttributeSet): RecyclerView(context, attrs) {

    //    var shouldInterTouchEvent = false
    var touchListener: OnTouchListener? = null

    private var startY = 0f
    private var startX = 0f

    private var isDragging = false

    var state = BottomSheetBehavior.STATE_HIDDEN//0: bottom, 1: half, 2: top
//    override fun onInterceptTouchEvent(event: MotionEvent?): Boolean {
//        when(event?.action) {
//            MotionEvent.ACTION_DOWN -> {
//                startX = event.x
//                startY = event.y
//                touchListener?.onTouch(this, event)
//            }
//            MotionEvent.ACTION_MOVE -> {
//                return if (state != BottomSheetBehavior.STATE_EXPANDED) {
//                    if ((isDragging || abs(event.y - startY) > abs(event.x - startX))) {
//                        isDragging = true
//                        true
//                    } else {
//                        super.onInterceptTouchEvent(event)
//                    }
//                } else if (isDragging || ((abs(event.y - startY) > abs(event.x - startX)) && event.y > startY && !canScrollVertically(-1))){
//                    //在顶部且向下滑动，子list不能向下滑动时浮层面板处理事件向下
//                    isDragging = true
//                    true
//                } else {
//                    super.onInterceptTouchEvent(event)
//                }
//            }
//            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
//                if (isDragging) {
//                    isDragging = false
//                    return true
//                }
//                return super.onInterceptTouchEvent(event)
//            }
//        }
//
//        return super.onInterceptTouchEvent(event)
//    }

//    override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
//        when(event?.action) {
//            MotionEvent.ACTION_DOWN -> {
//                startX = event.x
//                startY = event.y
//                touchListener?.onTouch(this, event)
//            }
//            MotionEvent.ACTION_MOVE -> {
//                return if (state != BottomSheetBehavior.STATE_EXPANDED) {
//                    if ((isDragging || abs(event.y - startY) > abs(event.x - startX))) {
//                        isDragging = true
//                        touchListener?.onTouch(this, event)
//                        true
//                    } else {
//                        super.dispatchTouchEvent(event)
//                    }
//                } else if (isDragging || ((abs(event.y - startY) > abs(event.x - startX)) && event.y > startY && !canScrollVertically(-1))){
//                    //在顶部且向下滑动，子list不能向下滑动时浮层面板处理事件向下
//                    isDragging = true
//                    touchListener?.onTouch(this, event)
//                    true
//                } else {
//                    super.dispatchTouchEvent(event)
//                }
//            }
//            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
//                if (isDragging) {
//                    isDragging = false
//                    touchListener?.onTouch(this, event)
//                    return true
//                }
//                return super.dispatchTouchEvent(event)
//            }
//        }
//
//        return super.dispatchTouchEvent(event)
//    }

//    override fun onTouchEvent(event: MotionEvent?): Boolean {
//        if (isDragging && touchListener != null && touchListener?.onTouch(this, event) == true) {
//            if (event?.action == MotionEvent.ACTION_CANCEL || event?.action == MotionEvent.ACTION_UP) {
//                isDragging = false
//            }
//            return true
//        } else {
//            if (event?.action == MotionEvent.ACTION_CANCEL || event?.action == MotionEvent.ACTION_UP) {
//                isDragging = false
//            }
//        }
//        return super.onTouchEvent(event)
//    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when(event?.action) {
            MotionEvent.ACTION_DOWN -> {
                startX = event.x
                startY = event.getY(0)
                touchListener?.onTouch(this, event)
            }
            MotionEvent.ACTION_MOVE -> {
                return if (state != BottomSheetBehavior.STATE_EXPANDED) {
                    if (isDragging || abs(event.getY(0) - startY) > 30) {
                        isDragging = true
                        touchListener?.onTouch(this, event)
                    }
                    true

                } else if (isDragging || ((abs(event.getY(0) - startY) > abs(event.x - startX)) && event.getY(0) > startY && !canScrollVertically(-1))){
                    //在顶部且向下滑动，子list不能向下滑动时浮层面板处理事件向下
                    isDragging = true
                    touchListener?.onTouch(this, event)
                    true
                } else {
                    super.onTouchEvent(event)
                }
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                if (isDragging) {
                    isDragging = false
                    touchListener?.onTouch(this, event)
                    return true
                }
                return super.onTouchEvent(event)
            }
        }
        return super.onTouchEvent(event)
    }
}